#pragma once 

#include <iostream>
#include <signal.h>
#include "Tcpserver.hpp"
#include <pthread.h>
#include "ThreadPool.hpp"

#include "Log.hpp"

class Httpserver{
private:
  int port;
  int lsock;
  bool stop;
public:
  Httpserver(int _port=8080):port(_port),stop(false)
  {}
  
  void Initserver()
  {
    //忽略信号的干扰
    signal(SIGPIPE,SIG_IGN);
  }

  void Loop()
  {
    Tcpserver* inst=Tcpserver::Getinstance(port);
    lsock=inst->Getsocket();
    while(!stop)
    {
      struct sockaddr_in peer;
      socklen_t len=sizeof(peer);
      int sock=accept(lsock,(struct sockaddr*)&peer,&len);
      if(sock<0){
        continue;
      }
      LOG(INFO,"get a new link");
      int* _sock=new int(sock);
      Task t(_sock);
      ThreadPool::GetInstance()->Pushtask(t);
    }
  }

  ~Httpserver()
  {}

};
